package com.lzj.admin.controller;

import com.lzj.admin.model.RespBean;
import com.lzj.admin.pojo.Role;
import com.lzj.admin.query.RoleQuery;
import com.lzj.admin.service.IRoleService;
import com.lzj.admin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author:王亚栋
 * @Date:2022/7/3 16:41
 * @Version:1.0
 */
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/index")
    public String index(){

        return "role/role";
    }

    /**
     * 角色列表
     * @param roleQuery
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> roleList(RoleQuery roleQuery){

       return roleService.roleList(roleQuery);

    }

    @RequestMapping("/addOrUpdateRolePage")
    public String addOrUpdateRolePage(Integer id, Model model){

        if (null != id){
            model.addAttribute("role",roleService.getById(id));
        }
        return "role/add_update";

    }

    @RequestMapping("/save")
    @ResponseBody
    public RespBean saveRole(Role role){

        roleService.saveRole(role);

        return RespBean.success("角色记录添加成功！");
    }

    @RequestMapping("/update")
    @ResponseBody
    public RespBean updateRole(Role role){

        roleService.updateRole(role);

        return RespBean.success("角色记录更新成功！");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public RespBean deleteRole(Integer id){

        roleService.deleteRole(id);

        return RespBean.success("角色记录删除成功！");
    }
}
